package com.cgt.gym.controller;

import com.cgt.gym.util.AliOSSUtil;
import com.cgt.gym.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@Controller
public class UploadController {

	//MultipartFile file封装了图片上传所有的信息
	@RequestMapping("/upload")
	@ResponseBody
	public Result upload(MultipartFile file) {
		//f7a9f3e6805a4e81b5d27245c6c30070
		String uuid = UUID.randomUUID().toString().replace("-", "");
		// a.png
		String filename = file.getOriginalFilename();
		// .png
		String extension = filename.substring(filename.lastIndexOf("."));
		//f7a9f3e6805a4e81b5d27245c6c30070.png
		String newFilename = uuid + extension;
		//一、本地上传
        /*String filePath = "D:\\mypic\\" + newFilename;
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }*/
		//二、阿里云oss上传
		String url = "";
		try {
			url = AliOSSUtil.uploadFile(newFilename, file.getInputStream());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

		return Result.ok("上传成功", url);
	}
}
